{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T07:23:52.288799Z",
     "start_time": "2025-08-25T07:23:52.283930Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "640d36be37dd0838",
   "outputs": [],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 这是我的第一个ipynb文件 演示数组求内积",
   "id": "73884dd6cdf0f5fe"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:08:11.218323Z",
     "start_time": "2025-08-25T07:08:11.208818Z"
    }
   },
   "source": [
    "# 这里是纯Python列表的计算\n",
    "# 需求： 求my_list1 + my_list2之和\n",
    "my_list1 = [1,2,3,4]\n",
    "my_list2 = [5,6,7,8]\n",
    "\n",
    "print(my_list1 + my_list2) # 这是将两个列表合并了，不是加法运算\n",
    "\n",
    "print('-' * 30)\n",
    "\n",
    "my_list3 = []\n",
    "for i in range(len(my_list1)):\n",
    "    my_list3.append(my_list1[i] + my_list2[i])\n",
    "\n",
    "print(my_list3)\n",
    "\n",
    "\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8]\n",
      "------------------------------\n",
      "[6, 8, 10, 12]\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T07:18:20.376188Z",
     "start_time": "2025-08-25T07:18:20.368100Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 这里是纯Numpy数组进行的计算\n",
    "a = np.array([1,2,3,4])\n",
    "b = np.array([5,6,7,8])\n",
    "\n",
    "print(a + b)  # 两个数组相加\n",
    "\n",
    "\n",
    "print(a * b) # 逐元素相乘，矩阵中 * 是求内积\n",
    "\n",
    "print(sum(a * b)) # 求内积\n",
    "print(a @ b) # 求内积\n",
    "print(np.dot(a,b)) # 求内积\n"
   ],
   "id": "e83f7142ec9a17ae",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 6  8 10 12]\n",
      "[ 5 12 21 32]\n",
      "70\n",
      "70\n",
      "70\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2. 数组计算初体验",
   "id": "da5afcfe785a8edf"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 需求 数组a与数组b相加，数组a是1 ~ N数字的立方，数组b是1 ~ N数字的平方",
   "id": "7a413e1551a2ca5f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T07:30:36.855427Z",
     "start_time": "2025-08-25T07:30:36.848587Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def arradd(n):\n",
    "    # 列表推导式\n",
    "    a = [i ** 3 for i in range(1,n)]\n",
    "    b = [i ** 2 for i in range(1,n)]\n",
    "    print(a)\n",
    "    print(b)\n",
    "    c = []\n",
    "    for i in range(n-1):\n",
    "        c.append(a[i] + b[i])\n",
    "    return c\n",
    "\n",
    "\n",
    "c = arradd(8)\n",
    "print(c)\n",
    "\n",
    "\n"
   ],
   "id": "b0935ac02f570440",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 8, 27, 64, 125, 216, 343]\n",
      "[1, 4, 9, 16, 25, 36, 49]\n",
      "[2, 12, 36, 80, 150, 252, 392]\n"
     ]
    }
   ],
   "execution_count": 22
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
